﻿Type=StaticCode
Version=1.90
@EndOfDesignText@
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
	'These global variables will be declared once when the application starts.
	'These variables can be accessed from all modules.

  'Dim actGrid As Activity
	'Dim scvList As ScrollView
  Dim pnlHeader As Panel
  Dim pnlTable As Panel
  Dim NumberOfColumns, RowHeight, RowHeight_1, ColumnWidth, ColumnWidth_1 As Int
  Dim HeaderColor, LineColor, CellColor, FontColor, HeaderFontColor As Int
  Dim ColLineWidth, RowLineWidth As Int
  Dim FontSize As Float
  Type RowCol (Row As Int, Col As Int)
  Dim Alignment As Int
  Dim SelectedRow As Int
  Dim SelectedRowColor As Int : SelectedRowColor=Colors.LightGray
	
	
  'Table settings
  HeaderColor = Colors.Gray
  NumberOfColumns = 2 'will be overwritten when loading from CSV file.
  ColLineWidth = 1dip
  RowLineWidth = 1dip
  RowHeight_1 = 30dip
  RowHeight=RowHeight_1+RowLineWidth
  LineColor = Colors.Black
  CellColor = Colors.White
  FontColor = Colors.Black
  HeaderFontColor = Colors.White
  FontSize = 14
  Alignment = Gravity.CENTER 'change to Gravity.LEFT or Gravity.RIGHT for other alignments.
End Sub

Sub LoadData(AData As String)
	Dim mapResult,mapRow As Map
	Dim lsRecords As List
	Dim result,code,key As String
	
	ClearAll
	
	PublicVar.GV_JSonTool.Initialize(AData)
	mapResult = PublicVar.GV_JSonTool.NextObject
	code = mapResult.Get("code")
	If code="0" Then
		result = mapResult.Get("result")
		PublicVar.GV_JSonTool.Initialize(result)
		lsRecords = PublicVar.GV_JSonTool.NextArray
		
		For i =0 To lsRecords.Size-1
			mapRow = lsRecords.Get(i)
			Dim h(mapRow.Size) As String
			For j = 0 To mapRow.Size -1 
				h(j) = mapRow.GetKeyAt(j)
				
			Next
			Exit	'只循环一次，取表头
		Next
		
		NumberOfColumns = h.Length
	  ColumnWidth = scvList.Width / 5
	  ColumnWidth_1 = ColumnWidth-ColLineWidth
	  SetHeader(h)		
		
		Dim data(NumberOfColumns) As String
		For i=0 To lsRecords.Size-1
			Dim row() As String
			mapRow = lsRecords.Get(i)
			For j=0 To NumberOfColumns -1
				key = h(j)		
				data(j) = mapRow.Get(key)
			Next			
			row =data
      AddRow(row)
		Next
	End If

End Sub

Sub ClearAll
    For i = pnlTable.NumberOfViews -1 To 0 Step -1
        pnlTable.RemoveViewAt(i)
    Next
    pnlTable.Height = 0
    SelectedRow = -1
End Sub

Sub SetHeader(Values() As String)
    If pnlHeader.IsInitialized Then Return 'should only be called once
    pnlHeader.Initialize("")
    For i = 0 To NumberOfColumns - 1
        Dim l As Label
        l.Initialize("header")
        l.Text = Values(i)
        l.Gravity = Gravity.CENTER
        l.TextSize = FontSize
        l.Color = HeaderColor
        l.TextColor = HeaderFontColor
        l.Tag = i
        pnlHeader.AddView(l, ColumnWidth * i, 0, ColumnWidth_1, RowHeight_1)
    Next
    'actGrid.AddView(pnlHeader, scvList.Left, scvList.Top - RowHeight, scvList.Width, RowHeight)
End Sub

Sub AddRow(Values() As String)
    If Values.Length <> NumberOfColumns Then
        Log("Wrong number of values.")
        Return
    End If
    Dim lastRow As Int
    lastRow = NumberOfRows
    For i = 0 To NumberOfColumns - 1
        Dim l As Label
        l.Initialize("cell")
        l.Text = Values(i)
        l.Gravity = Alignment
        l.TextSize = FontSize
        l.TextColor = FontColor
        l.Color=Colors.White
        Dim rc As RowCol
        rc.Initialize
        rc.Col = i
        rc.Row = lastRow
        l.Tag = rc
        pnlTable.AddView(l, ColumnWidth * i, RowHeight * lastRow, ColumnWidth_1, RowHeight_1)
    Next
    pnlTable.Height = NumberOfRows * RowHeight
End Sub

Sub NumberOfRows As Int
    Return pnlTable.NumberOfViews / NumberOfColumns
End Sub
Sub Cell_Click
    Dim rc As RowCol
    Dim l As Label
    Dim l0 As Label
    l = Sender
    rc = l.Tag
    SelectRow(rc.Row)
    l0=GetView(rc.Row,0)
    'actGrid.Title = "Cell clicked: (" & rc.Row & ", " & rc.Col & l0.Text &")"     
End Sub

Sub Header_Click
    Dim l As Label
    Dim col As Int
    l = Sender
    col = l.Tag
    'actGrid.Title = "Header clicked: " & col
End Sub

Sub SelectRow(Row As Int)
    'remove the color of previously selected row
    If SelectedRow > -1 Then
        For col = 0 To NumberOfColumns - 1
            GetView(SelectedRow, col).Color = CellColor
        Next
    End If
    SelectedRow = Row
    For col = 0 To NumberOfColumns - 1
        GetView(Row, col).Color = SelectedRowColor
    Next
End Sub

'Returns the label in the specific cell
Sub GetView(Row As Int, Col As Int) As Label
    Dim l As Label
    l = pnlTable.GetView(Row * NumberOfColumns + Col)
    Return l
End Sub